package com.dongdongshop.service.impl.goods_jpa;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.dao.goods_desc_jpa.GoodsDescJpaDao;
import com.dongdongshop.dao.goods_jpa.GoodsJpaDao;
import com.dongdongshop.dao.item_jpa.ItemJpaDao;
import com.dongdongshop.pojo.goods.TbGoods;
import com.dongdongshop.pojo.goods_desc.TbGoodsDesc;
import com.dongdongshop.pojo.item.TbItem;
import com.dongdongshop.pojo.seller.TbSeller;
import com.dongdongshop.service.goods_jpa.GoodsJpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.*;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class GoodsJpaServiceImpl implements GoodsJpaService {
    @Autowired
    private GoodsJpaDao goodsJpaDao;
    @Autowired
    private GoodsDescJpaDao goodsDescJpaDao;
    @Autowired
    private ItemJpaDao itemJpaDao;
    @Override
    @Transactional
    public void insertGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, List<TbItem> tbItems, TbSeller tbSeller) {
        TbGoods saveGoods = goodsJpaDao.save(tbGoods);
        tbGoodsDesc.setGoodsId(saveGoods.getId());
        TbGoodsDesc save = goodsDescJpaDao.save(tbGoodsDesc);
        System.out.println(save);
        List<TbItem> list = setListItems(tbGoods, tbGoodsDesc, tbItems);
        for (TbItem tbItem : list) {
            tbItem.setSellerId(tbGoods.getSellerId());
            tbItem.setSeller(tbSeller.getName());
            itemJpaDao.save(tbItem);
        }
    }

    @Override
    @Transactional
    public void updateAudit(Long[] ids) {
        for (Long id : ids) {
            TbGoods goods = goodsJpaDao.getOne(id);
            goods.setAuditStatus("2");
            goodsJpaDao.save(goods);
        }
    }

    @Override
    public List<TbGoods> listGoodsByUnreviewed() {
        return goodsJpaDao.findAllByAuditStatus("2");
    }

    @Override
    @Transactional
    public void updateAuditToPass(Long[] ids) {
        for (Long id : ids) {
            TbGoods goods = goodsJpaDao.getOne(id);
            goods.setAuditStatus("1");
            goodsJpaDao.save(goods);
        }
    }

    @Override
    public TbGoods getGoodsById(Long goodsId) {
        return goodsJpaDao.getGoodsbyId(goodsId);
    }

    private List<TbItem> setListItems(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc,List<TbItem> tbItems){
        List<TbItem> list = new ArrayList<>();
        for (TbItem tbItem : tbItems) {
            String spec = tbItem.getSpec();
            JSONObject object = JSON.parseObject(spec);
            Map<String, Object> innerMap = object.getInnerMap();
            Set<Map.Entry<String, Object>> entrys = innerMap.entrySet();  //此行可省略，直接将map.entrySet()写在for-each循环的条件中
            String title = tbGoods.getGoodsName();
            for(Map.Entry<String, Object> entry:entrys){
                title+=entry.getKey()+entry.getValue();
            }
            tbItem.setTitle(title);
            tbItem.setGoodsId(tbGoods.getId());
            tbItem.setStockCount(tbItem.getNum());
            tbItem.setCategoryid(tbGoods.getCategory3Id());
            tbItem.setCreateTime(new Date());
            tbItem.setUpdateTime(new Date());
            list.add(tbItem);

        }
        return list;
    }
}
